package com.jishuo.nontax.bfc.tlq;

import com.jishuo.base.config.NonTaxConfig;
import com.tongtech.tmqi.XAQueueConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.annotation.Resource;

/**
 * TLQ Jms 启动配置
 */
@Configuration
public class JmsConfig {

    @Resource
    private NonTaxConfig nonTaxConfig;

    @Bean(name = "desinationResolver")
    public TlqDestinationResolver tlqDestinationResolver() {
        TlqDestinationResolver tlqDestinationResolver = new TlqDestinationResolver();
        return tlqDestinationResolver;
    }

    @Bean(name = "tlqConnectFactory")
    public XAQueueConnectionFactory tlqConnectFactory() throws Exception {
        XAQueueConnectionFactory tlqConnectFactory = new XAQueueConnectionFactory();
        tlqConnectFactory.setProperty("tmqiAddressList", "tlkq://" + nonTaxConfig.TLQ_IP + ":" + nonTaxConfig.TLQ_Port);
        return tlqConnectFactory;
    }

    // JMS队列模板，发消息用
    @Bean(name = "jmsQueueTemplate")
    public JmsTemplate jmsQueueTemplate(
            @Qualifier("tlqConnectFactory") XAQueueConnectionFactory mqConnectionFactory,
            @Qualifier("desinationResolver") TlqDestinationResolver desinationResolver
    ) {
        JmsTemplate bean = new JmsTemplate();
        bean.setConnectionFactory(mqConnectionFactory);
        bean.setPubSubDomain(false);
        bean.setSessionTransacted(false);
        bean.setSessionAcknowledgeMode(1);
        bean.setDestinationResolver(desinationResolver);
        bean.setReceiveTimeout(20000);
        return bean;
    }

    // 消息监听
    @Bean(name = "jmsContainer")
    public DefaultMessageListenerContainer jmsContainer(
            @Qualifier("tlqConnectFactory") XAQueueConnectionFactory mqConnectionFactory,
            @Qualifier("desinationResolver") TlqDestinationResolver desinationResolver,
            @Qualifier("jmsReceive") JmsReceive jmsReceive
    ) {
        if (nonTaxConfig.DebugType != 0) return null;
        jmsReceive.setQueue("l_nontax");
        DefaultMessageListenerContainer bean = new DefaultMessageListenerContainer();
        bean.setConnectionFactory(mqConnectionFactory);
        bean.setDestinationResolver(desinationResolver);
        bean.setConcurrentConsumers(1);
        bean.setMessageListener(jmsReceive);
        bean.setSessionTransacted(false);
        bean.setSessionAcknowledgeMode(1);
        bean.setDestinationName(jmsReceive.getQueue());
        return bean;
    }

    // 消息监听
    @Bean(name = "jmsContainer2")
    public DefaultMessageListenerContainer jmsContainer2(
            @Qualifier("tlqConnectFactory") XAQueueConnectionFactory mqConnectionFactory,
            @Qualifier("desinationResolver") TlqDestinationResolver desinationResolver,
            @Qualifier("jmsReceive") JmsReceive jmsReceive
    ) {
        if (nonTaxConfig.DebugType != 0) return null;
        jmsReceive.setQueue("l_base");
        DefaultMessageListenerContainer bean = new DefaultMessageListenerContainer();
        bean.setConnectionFactory(mqConnectionFactory);
        bean.setDestinationResolver(desinationResolver);
        bean.setConcurrentConsumers(1);
        bean.setMessageListener(jmsReceive);
        bean.setSessionTransacted(false);
        bean.setSessionAcknowledgeMode(1);
        bean.setDestinationName(jmsReceive.getQueue());
        return bean;
    }
}
